package com.pb.wkflow.core.utils;

import de.odysseus.el.util.SimpleContext;

import javax.el.ExpressionFactory;
import javax.el.PropertyNotFoundException;
import javax.el.ValueExpression;
import java.util.Map;

/**
 * juel 解析器
 * @author 苗益辉
 * @date 2022/5/24 13:50
 */
public class JuelUtils {

    public static boolean checkFormDataByRuleEl(String textContent, Map<String, Object> variables) {
        ExpressionFactory factory = ExpressionFactory.newInstance();
        SimpleContext context = new SimpleContext();
        for (Map.Entry<String,Object> entry : variables.entrySet())  {
            Object value = entry.getValue();
            if (value != null) {
                context.setVariable(entry.getKey(), factory.createValueExpression(value, value.getClass()));
            }
        }

        ValueExpression e = factory.createValueExpression(context, textContent, Boolean.class);
        try {
            return (Boolean) e.getValue(context);
        }catch (PropertyNotFoundException e1) {
            return false;
        }
    }
}
